﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Authorization;
using NetCoreCMS.IServices;
using NetCoreCMS.Models;

namespace NetCoreCMS.Web.Areas.WebApi.Controllers
{
    public class Favorite_AddController : UserController
    {
        public IFavoriteService _favorite { get; set; }
        public IArticleService _article { get; set; }
        public IShopService _shop { get; set; }
        [Authorize]
        [HttpGet]
        public IActionResult Get(int like_id,string title,string remark,int ftype)
        {
            var user = GetUserInfo();
            if (user == null)
            {
                return Ok(new { status = 0, msg = "请先登录！" });
            }
            //先查可存在
            var list = _favorite.GetList(1, 1, "where user_id=" + user.id + " and like_id=" + like_id + " and ftype=" + ftype,"id asc");
            if (list.Count==0)
            {
                string img_url = "";
                decimal sell_price = 0;
                if (ftype == 2)
                {
                    var product = _article.GetModel(like_id);
                    img_url = product.img_url;
                    sell_price = product.sell_price;
                }
                if(ftype==3)
                {
                    var shop = _shop.GetModel(like_id);
                    img_url = shop.img_url;
                }
                Favorite fav = new Favorite();
                fav.user_id = user.id;
                fav.like_id = like_id;
                fav.title = title;
                fav.ftype = ftype;
                fav.remark = remark;
                fav.img_url = img_url;
                fav.sell_price = sell_price;
                fav.id=_favorite.Add(fav);
                return Ok(new { status = 1, msg = "收藏成功！", data = fav,shoucang=1 });
            }
            else
            {
                return Ok(new { status = 1, msg = "已经存在！",shoucang=1 });
            }
        }
    }
}